package com.example.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.reggie.common.R;
import com.example.reggie.entity.Employee;
import com.example.reggie.service.EmployeeService;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.DigestUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.time.LocalDateTime;

@RestController
@RequestMapping("/employee")
@Api(tags = "员工登录")
public class EmployeeController {

    /*自动注入*/
    @Autowired
   private EmployeeService employeeService;

    /*用于登录，post请求,发送的是json请求那么就需要@RequestBody,获取到的是一个对象*/
    @PostMapping("/login")
    public R<Employee> login(HttpServletRequest request, @RequestBody Employee employee){

        //1、将页面提交的密码password进行md5加密处理
        String password = employee.getPassword();
        password = DigestUtils.md5DigestAsHex(password.getBytes());

        /*获取名字*/
        String name = employee.getUsername();
        /*name是管理员不是名字*/
//        String name = employee.getName();

        LambdaQueryWrapper<Employee> wrapper = new LambdaQueryWrapper<>();
        /*判断账号账号是否存在*/
        wrapper.eq(name!=null,Employee::getUsername,name);
//        wrapper.eq(name!=null,Employee::getName,name);
//                eq(password!=null,Employee::getPassword,password)

        /*获取值*/
        Employee one = employeeService.getOne(wrapper);


        /*判断有没有查询到该用户，没有就登录失败*/
        if (one==null){
            return R.error("登录失败");
            /**/
        }

        /*判断密码是否正确，可以查询到值就代表着账号名是正常的所以不用去判断是否正常或者错误*/
        if (!one.getPassword().equals(password)){
            return R.error("密码错误");
        }
        /*判断用户的状态,0为禁用*/
        if (one.getStatus()==0){
            return R.error("该用户已被封禁");
        }

        /*放入服务器中*/        /*要放入的内容*/
        request.getSession().setAttribute("employee",one.getId());
        /*返回数据库查询到之后的内容*/
        return R.success(one);
    }

    /*退出*/
    @PostMapping("/logout")
    public R<String> logout(HttpServletRequest request){
        request.getSession().removeAttribute("employee");

        return R.success("退出成功");
    }
    /*添加 username设置了唯一*/
    @PostMapping
    public R<String> add(HttpServletRequest request,@RequestBody Employee employee){
        System.out.println("一开始的"+employee);

        /*添加时间,系统时间*/
        employee.setCreateTime(LocalDateTime.now());
        employee.setUpdateTime(LocalDateTime.now());

        /*加密操作*/
        String md5Password = DigestUtils.md5DigestAsHex("12345".getBytes());
        employee.setPassword(md5Password);

        /*获取操作人谁*/
        long id = (Long)request.getSession().getAttribute("employee");

        employee.setCreateUser(id);
        employee.setUpdateUser(id);


        System.out.println("添加之后的:" + employee);

        employeeService.save(employee);

        return R.success("添加成功");
    }


    /*分页查询和模糊查询*/

    /**
     * 员工信息分页查询
     * @param page 当前查询页码
     * @param pageSize 每页展示记录数
     * @param name 员工姓名 - 可选参数
     * @return
     */
    @GetMapping("/page")
    public R<IPage> page(Integer page, Integer pageSize, String name){

        LambdaQueryWrapper<Employee> wrapper = new LambdaQueryWrapper<>();
        wrapper.like(name!=null,Employee::getName,name);

        IPage<Employee> iPage = employeeService.page(new Page<>(page, pageSize), wrapper);
        return R.success(iPage);
    }

    /*员工的启用和禁用,0为禁用,1为启用*/
    @PutMapping
    public R<String> updateStatus(HttpServletRequest request,@RequestBody Employee employee){

        Long empId = (Long)request.getSession().getAttribute("employee");

        employee.setUpdateTime(LocalDateTime.now());
        employee.setUpdateUser(empId);

        employeeService.updateById(employee);
        return R.success("修改成功");
    }

    /*查询员工*/
    @GetMapping("/{id}")
    public R<Employee> selectById(@PathVariable Long id){

        LambdaQueryWrapper<Employee> wrapper = new LambdaQueryWrapper<>();
        wrapper.like(Employee::getId,id);

        Employee one = employeeService.getOne(wrapper);


        return R.success(one);
    }
}
